package jman.teemwork14;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
	public User getUserByUserName(String userName) throws SQLException{
		Dao d=new Dao();
		d.start();
		String sql="select * from user where username='"+userName+"'";
		String id="";
		ResultSet rs=d.read(sql);
		while(rs.next()){
			id=rs.getString("id");
		}
		User u=new User(userName,id);
		d.close();
		return u;
	}
	public boolean isVote(String id) throws SQLException{
		boolean flag=false;
		Dao d=new Dao();
		d.start();
		String sql="select isvote from voteuser where id='"+id+"'";
		ResultSet rs=d.read(sql);
		while(rs.next()){
			if(rs.getInt("isvote")==0){
				flag=true;
			}
		}
		d.close();
		return flag;
	}
	public boolean vote(String id,String ID){
		boolean flag=false;
		Dao d=new Dao();
		d.start();
		String sql1="update voteuser set isvote=1 where id='"+id+"'";
		boolean b1=d.update(sql1);
		String sql2="update voteduser set number=number+1 where id='"+ID+"'";
		boolean b2=d.update(sql2);
		if(b1&&b2){
			flag=true;
		}
		d.close();
		return flag;
	}
}
